/*
** Copyright (c) 2008 Mobile GCalendar
**
** Permission is hereby granted, free of charge, to any person
** obtaining a copy of this software and associated documentation
** files (the "Software"), to deal in the Software without
** restriction, including without limitation the rights to use,
** copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, and to permit persons to whom the
** Software is furnished to do so, subject to the following
** conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
** WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
** OTHER DEALINGS IN THE SOFTWARE.
*/

package au.edu.mq.comp.itec800.mgc.util;

import java.util.Date;

import com.google.gdata.data.BaseEntry;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.TextConstruct;
import com.google.gdata.data.ValueConstruct;
import com.google.gdata.data.calendar.ColorProperty;
import com.google.gdata.data.calendar.HiddenProperty;
import com.google.gdata.data.calendar.TimeZoneProperty;

/**
 * Convenience class for manipulating data structure and objects used by
 * the Google Data APIs.
 *
 * @author Laurent Malvert [laurent.malvert@students.mq.edu.au]
 */
public class GDataUtils
{
  public static Date              fromDateTime(final DateTime dt)
  {
    Date                          date = new Date();

    if (dt != null)
    {
      date = new Date(dt.getValue());
    }
    return (date);
  }

  public static String            fromTextConstruct(final TextConstruct tc)
  {
    String                        content = "";

    if (tc != null)
    {
      content = StringUtils.sanitize(tc.getPlainText());
    }
    return (content);
  }

  public static String            fromValueConstruct(final ValueConstruct vc)
  {
    String                        content = "";

    if (vc != null)
    {
      content = StringUtils.sanitize(vc.getValue());
    }
    return (content);
  }

  public static String            fromColorProperty(final ColorProperty cp)
  {
    return (fromValueConstruct(cp));
  }

  public static String            fromTimeZoneProperty(final TimeZoneProperty tzp)
  {
    return (fromValueConstruct(tzp));
  }

  public static String            getSafeId(final BaseEntry<?> be)
  {
    String                        id = "";

    if (be != null)
    {
      final String  oid   = be.getId();

      if (oid != null)
      {
        final int     start = oid.lastIndexOf("/");

        id = (start != -1) ? oid.substring(start) : oid;
      }
    }
    return (id);
  }

  public static TextConstruct     toPlainTextConstruct(final String str)
  {
    return (new PlainTextConstruct(StringUtils.sanitize(str)));
  }

  public static ColorProperty     toColorProperty(final String str)
  {
    return (new ColorProperty(StringUtils.clean(str)));
  }

  public static TimeZoneProperty  toTimeZoneProperty(final String str)
  {
    return (new TimeZoneProperty(StringUtils.clean(str)));
  }

  public static HiddenProperty    toHiddenProperty(final String str)
  {
    return (StringUtils.clean(str).toLowerCase().equalsIgnoreCase("hidden") ? HiddenProperty.TRUE : HiddenProperty.FALSE);
  }
}
